package com.tongcao.cn.dfire.result;

import com.tongcao.cn.dfire.resultCode.BaseResultCode;
import com.tongcao.cn.dfire.resultCode.ResultCode;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author:tongcao
 * @Date:2018/12/27 20:26
 * Description:
 */
public class ResultSupport<T> implements Result<T> {

    private boolean success = true;
    private String resultCode;
    private String message;
    private T model;
    private int totalRecord;


    private List<T> models = new ArrayList<T>();

    @Override
    public String getMessage() {
        return message;
    }

    @Override
    public void setMessage(String message) {
        this.message = message;
    }

    @Override
    public T getModel() {
        return model;
    }

    @Override
    public int getTotalRecord() {
        return totalRecord;
    }

    @Override
    public void setTotalRecord(int totalRecord) {
        this.totalRecord = totalRecord;
    }

    /**
     * 创建一个result。
     */
    public ResultSupport() {
    }

    /**
     * 创建一个result。
     *
     * @param success 是否成功
     */
    public ResultSupport(boolean success) {
        this.success = success;
    }

    public ResultSupport(boolean success, String resultCode, String message) {
        this.success = success;
        this.resultCode = resultCode;
        this.message = message;
    }

    public ResultSupport(String resultCode, String message) {
        this.success = Boolean.FALSE;
        this.resultCode = resultCode;
        this.message = message;
    }

    public ResultSupport(ResultCode resultCode) {
        this.success = false;
        this.resultCode = resultCode.getCode();
        this.message = resultCode.getMessage();
    }

    @Override
    public boolean isSuccess() {
        return success;
    }


    @Override
    public void setSuccess(boolean success) {
        this.success = success;
    }


    /**
     * @return the resultCode
     */
    @Override
    public String getResultCode() {
        if (!isSuccess() && StringUtils.isBlank(resultCode)) {
            return BaseResultCode.SYSTEM_DEFAULT.getCode();
        }
        return resultCode;
    }

    /**
     * @param resultCode the resultCode to set
     */
    @Override
    public void setResultCode(String resultCode) {
        this.resultCode = resultCode;
    }

    @Override
    public List<T> getModels() {
        return models;
    }

    @Override
    public void setModels(List<T> models) {
        this.models = models;
    }

    @Override
    public void setModel(T model) {
        this.model = model;
    }
}
